package com.infomanage.service.plan;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.PreventcheckPlanDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.PreventcheckPlan;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.FileUpload;
import com.infomanage.util.StringUtil;
import com.infomanage.util.smartupload.SmartRequest;
import com.infomanage.util.smartupload.SmartUpload;
import com.infomanage.vo.EquipmentPlanItem;
import com.infomanage.vo.PreventcheckPlanItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

/**
 * 矿井预防检查计划
 *
 */
public class PreventCheckPlanService {

	public static PreventcheckPlanItem loadPreventCheckPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String planId= request.getParameter("checkId");
			if(StringUtils.isEmpty(planId)) return null;
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			return dao.find(Integer.parseInt(planId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<PreventcheckPlanItem> loadPreventCheckPlanList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			List<PreventcheckPlanItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject savePreventCheckPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			PreventcheckPlan plan = new PreventcheckPlan();
			plan.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			plan.setPersonId(request.getParameter("personId"));
			plan.setPersonName(request.getParameter("personname"));
			plan.setPlace(request.getParameter("place"));
			plan.setRoute(request.getParameter("route"));
			plan.setImgDir(request.getParameter("imgDir"));
			plan.setGasCh4(request.getParameter("gasCh4"));
			plan.setGasCo(request.getParameter("gasCo"));
			plan.setGasCo2(request.getParameter("gasCo2"));
			plan.setGasO2(request.getParameter("gasO2"));
			plan.setGasT(request.getParameter("gasT"));
			plan.setProblem(request.getParameter("problem"));
			plan.setDealResult(request.getParameter("dealResult"));
			plan.setTimestamp(request.getParameter("timestamp"));
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			dao.add(plan);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static ReturnObject modifyPreventCheckPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("checkId"));
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			PreventcheckPlan plan = dao.find(PreventcheckPlan.class, planId);
			plan.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			plan.setPersonName(request.getParameter("personname"));
			plan.setPersonId(request.getParameter("personId"));
			plan.setPlace(request.getParameter("place"));
			plan.setRoute(request.getParameter("route"));
			plan.setImgDir(request.getParameter("imgDir"));
			plan.setGasCh4(request.getParameter("gasCh4"));
			plan.setGasCo(request.getParameter("gasCo"));
			plan.setGasCo2(request.getParameter("gasCo2"));
			plan.setGasO2(request.getParameter("gasO2"));
			plan.setGasT(request.getParameter("gasT"));
			plan.setProblem(request.getParameter("problem"));
			plan.setDealResult(request.getParameter("dealResult"));
			plan.setTimestamp(request.getParameter("timestamp"));
			dao.modify(plan);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static ReturnObject removePreventCheckPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String planId = request.getParameter("checkId");
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			dao.remove(PreventcheckPlan.class, Integer.parseInt(planId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static List<PreventcheckPlanItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<PreventcheckPlanItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject removeBatchPreventCheckPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(PreventcheckPlan.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
	
	public static ReturnObject uploadLegend(HttpServletRequest httpRequest, HttpServletResponse response, PageContext pageContext){
		ReturnObject retObj = new ReturnObject();
		try {
			httpRequest.setCharacterEncoding("UTF-8");
			String actionType = httpRequest.getParameter("actionType");
			if (actionType == null) {
				FileUpload fileUpload = new FileUpload("jpg,gif,png,bmp,dwg");
				SmartUpload smartUpload = fileUpload.upload(httpRequest,
						pageContext);
				SmartRequest request = smartUpload.getRequest();
				actionType = request.getParameter("actionType");
				String[] fileNames = smartUpload.getFileNames();
				if (fileNames != null && fileNames.length > 0) {
					String dir = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
					retObj.SetErrored(false);
					retObj.setErrorMessage(dir);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			retObj.setErrorMessage(e.getMessage());
		}
		return retObj;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			PreventcheckPlanDAO dao = new PreventcheckPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String deptId = request.getParameter("deptId");
		String personId = request.getParameter("personId");
		String timestamp = request.getParameter("timestamp");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(deptId)){
			FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(personId)){
			FieldBean field = new FieldBean("personId", personId);
			field.setLike(true);
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(timestamp)){
			FieldBean field = new FieldBean("timestamp", timestamp);
			searchBean.add(field);
		}
		
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if(user != null){
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
